/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import BusinessLogic.MyQueryBL;
import Ch.Elca.Iiop.GenericUserException;
import Model.CustomDataType.CMDateTime;
import Model.CustomDataType._CMDateTimeStub;
import corba.CorbaConfig;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Paul
 */
public class Util {

    public static Date convertCMDateTimeToDate(CMDateTime cm) {
        Calendar c = Calendar.getInstance();
        try {
            c.set(cm.getYear(), cm.getMonth() - 1, cm.getDay());
            return c.getTime();
        } catch (GenericUserException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static CMDateTime convertDateToCMDateTime(MyQueryBL m, Date d) {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        try {
            return m.getCMDateTime(c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH));
        } catch (GenericUserException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static void saveConfig(CorbaConfig config) throws FileNotFoundException, IOException {
        File f = new File("temp/config.pmt");
        FileOutputStream fout = new FileOutputStream(f);
        try (ObjectOutputStream oos = new ObjectOutputStream(fout)) {
            oos.writeObject(config);
            oos.flush();
        }
    }
    public static CorbaConfig loadfConfig() throws FileNotFoundException, IOException, ClassNotFoundException
    {
        File f = new File("temp/config.pmt");
        FileInputStream fiut = new FileInputStream(f);
        try (ObjectInputStream ois = new ObjectInputStream(fiut)) {
            return (CorbaConfig) ois.readObject();
        }
    }
}
